// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Gunnar Ritter

#include "config/applemail_plist_test.h"
#include "config/applemail_plist.h"

namespace applemail_plist_test {

void TestAppleMailPlist::TestMissingFile() {
  AppleMailPlist plist;
  QVERIFY(plist.Read("/a/b/c/d/e/f/g") == false);
}

void TestAppleMailPlist::TestBadPlistStructure() {
  AppleMailPlist plist;
  QVERIFY(plist.Read("com.apple.mail.plist.bad") == false);
}

void TestAppleMailPlist::TestGoodPlistResult() {
  AppleMailPlist plist;
  QVERIFY(plist.Read("com.apple.mail.plist") == true);
  QVERIFY(plist.accounts_version() == 23);
  int mail_accounts_tested = 0;
  for (int i = 0; i < plist.mail_accounts().size(); ++i) {
    const struct AppleMailPlist::MailAccount *acc =
        &plist.mail_accounts().at(i);
    if (acc->AccountName == "Gmail") {
      ++mail_accounts_tested;
      QVERIFY(acc->AccountType == "POPAccount");
      QVERIFY(acc->DeleteMessagesOnServerInterval == 1);
      QVERIFY(acc->DeletionPolicy == "AfterInterval");
      QVERIFY(acc->EmailAddresses.at(0) == "diag.test.account@gmail.com");
      QVERIFY(acc->FullUserName == "Diag Test");
      QVERIFY(acc->Hostname == "pop.gmail.com");
      QVERIFY(acc->PortNumber == 995);
      QVERIFY(acc->SMTPIdentifier ==
              "smtp.gmail.com:diag.test.account@gmail.com");
      QVERIFY(acc->SSLEnabled == true);
      QVERIFY(acc->SecurityLayerType == 3);
      QVERIFY(acc->Username == "diag.test.account@gmail.com");
    } else if (acc->AccountName == "Gmail IMAP") {
      ++mail_accounts_tested;
      QVERIFY(acc->AccountType == "IMAPAccount");
      QVERIFY(acc->EmailAddresses.at(0) == "diag.test.account@gmail.com");
      QVERIFY(acc->FullUserName == "Diag Test");
      QVERIFY(acc->Hostname == "imap.gmail.com");
      QVERIFY(acc->PortNumber == 993);
      QVERIFY(acc->SMTPIdentifier ==
              "smtp.gmail.com:diag.test.account@gmail.com");
      QVERIFY(acc->SSLEnabled == true);
      QVERIFY(acc->SecurityLayerType == 3);
      QVERIFY(acc->Username == "diag.test.account@gmail.com");
    }
  }
  QVERIFY(mail_accounts_tested == 2);
  int delivery_accounts_tested = 0;
  for (int i = 0; i < plist.delivery_accounts().size(); ++i) {
    const struct AppleMailPlist::DeliveryAccount *acc =
        &plist.delivery_accounts().at(i);
    if (acc->Hostname == "smtp.gmail.com" &&
        acc->Username == "diag.test.account@gmail.com") {
      ++delivery_accounts_tested;
      QVERIFY(acc->AccountType == "SMTPAccount");
      QVERIFY(acc->SSLEnabled == true);
      QVERIFY(acc->SecurityLayerType == 2);
      QVERIFY(acc->ShouldUseAuthentication == true);
    }
  }
  QVERIFY(delivery_accounts_tested == 1);
}

}  // namespace applemail_plist_test

QTEST_MAIN(applemail_plist_test::TestAppleMailPlist)
